package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map(itme -> {
            itme.setSetmealId(setmealDto.getId());
            return itme;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }

    //删除套餐，同时需要删除套餐和菜品的关联数据
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 1);

        int count = this.count(lqw);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> slqw = new LambdaQueryWrapper<>();
        slqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(slqw);
    }

    @Override
    @Transactional
    //修改前回显数据
    public SetmealDto getByIdWithFlavor(Long id) {//controller层调用此方法，通过页面请求中传过来的套餐id就可以回显出套餐中对应的菜品
        //当前对象调用getByid方法，将页面接收到的id传进去，查询出一个套餐对象
        Setmeal setmeal = this.getById(id);
        //new出一个dto，要用里面的套餐菜品集合
        SetmealDto setmealDto = new SetmealDto();
        //将当前套餐对象拷贝给dto，因为要扩展
        BeanUtils.copyProperties(setmeal,setmealDto);

        //调用lambda条件查询，泛型为套餐菜品关联信息，因为要从里面获取到套餐id
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //根据套餐id查询对应的菜品套餐信息
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        //将满足条件的菜品套餐信息调用service接口方法添加到集合中
        List<SetmealDish> list = setmealDishService.list(lqw);
        //将查询出来的菜品套餐集合给dto设置进去
        setmealDto.setSetmealDishes(list);
        //返回dto
        return setmealDto;
    }

    @Override
    @Transactional
    //修改套餐数据
    public void updateWithDish(SetmealDto setmealDto) {
        //当前对象调用updateById方法修改当前套餐信息
        this.updateById(setmealDto);

        //光修改了套餐信息不行，套餐对应的菜品信息也要修改
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //根据套餐id查询出当前套餐所关联的菜品
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //将满足查询条件的菜品进行删除，方便后面添加
        setmealDishService.remove(lqw);

        //添加当前提交过来的菜品数据---setmeal_dish表的insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();//获取dto里面装有套餐菜品信息的集合，对其进行修改
        //将页面修改过后的套餐数据根据id设置给dto中的套餐菜品集合中
        List<SetmealDish> collect = setmealDishes.stream().map(itme -> {
            itme.setSetmealId(setmealDto.getId());
            return itme;
        }).collect(Collectors.toList());
        //将修改过后新的套餐数据调用service.saveBatch方法添加进去，controller层调用此修改方法，完成修改
        setmealDishService.saveBatch(collect);
    }
}
